from datetime import datetime
from apps.projectbases.models import VersionHistoryModel
from utils.utils import update_set


class VerisonHistoryService:
    """
    这个类是通用的处理版本履历的插入与删除的
    """
    def __init__(self, request) -> None:
        self.request = request
        super().__init__()

    def createVersionHistoryRequest(self, projectplan_list):
        projectplan_id = projectplan_list.id

        version_query = VersionHistoryModel.objects.filter(projectPlanFK__id=projectplan_id).order_by('-id')
        if version_query.exists():
            version = version_query[0].version
            version += 1
        else:
            version = 1

        current_datetime = datetime.now()
        version_data = {
            'projectPlanFK': projectplan_list,
            'version': version,
            'version_date': current_datetime,
            'note': None
        }
        version_project = VersionHistoryModel.objects.create(**version_data, **update_set(self.request, True))

        return version_project


    def deleteVersionHistoryRequest(self, projectplan_id):
        version_query = VersionHistoryModel.objects.filter(projectPlanFK__id=projectplan_id)

        if version_query.exists():
            version_query.delete()

